package com.service.back.impl;

import com.alibaba.fastjson.JSONObject;
import com.conf.BackAdminLogin;
import com.conf.BaseServlet;
import com.model.TAdminInfo;
import com.model.TAdminRole;
import com.model.TProxyInfo;
import com.service.back.TProxyInfoService;
import com.util.MD5Util;
import com.util.ResultMap;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TProxyInfoServiceImpl extends BaseServlet implements TProxyInfoService {

    @Value("${application.wx.url}")
    private String wxUurl;

    @Override
    public List<TProxyInfo> selectTProxyInfoList(JSONObject jsonObject, TAdminInfo adminInfo) {
        if(adminInfo.getTaiGroupId() == null || adminInfo.getTaiGroupId() == 3){
            return null;
        }
        return tProxyInfoMapper.selectTProxyInfoList(
                jsonObject.getInteger("tpiHeadId"),
                jsonObject.getString("tpiPhone"),
                jsonObject.getString("tpiAccount"),
                adminInfo.getTaiGroupId(),
                adminInfo.getTaiRelationId()
        );
    }

    @Transactional
    @Override
    public Map<String, Object> addTProxyInfo(TProxyInfo tProxyInfo) throws Exception{
        Map<String, Object> map = new HashMap<>();
        TAdminInfo adminInfo1 = BackAdminLogin.getAdminInfo();
        if(adminInfo1.getTaiGroupId() == null || adminInfo1.getTaiGroupId() == 3){
            map.put("code",105);
            map.put("msg","账号登录异常，请重新登录后操作！");
            return map;
        }
        //判断参数是否齐全
        if(StringUtil.isBlank(tProxyInfo.getTpiAccount()) || StringUtil.isBlank(tProxyInfo.getPwd1()) ||
                StringUtil.isBlank(tProxyInfo.getPwd2()) || StringUtil.isBlank(tProxyInfo.getTpiPhone()) ||
                tProxyInfo.getTpiRatio() == null || StringUtil.isBlank(tProxyInfo.getTpiName()) ||
                StringUtil.isBlank(tProxyInfo.getTpiBankCard()) || StringUtil.isBlank(tProxyInfo.getTpiBankName())){
            return ResultMap.errorParam();
        }

        //判断用户名是否存在
        List<TProxyInfo> accountList = tProxyInfoMapper.selectByAccount(tProxyInfo.getTpiAccount(),null);
        if(accountList.size() != 0){
            map.put("code",102);
            map.put("msg","该用户名已被注册");
            return map;
        }

        //判断手机号码是否被注册
        List<TProxyInfo> phoneList = tProxyInfoMapper.selectByPhone(tProxyInfo.getTpiPhone(),null);
        if(phoneList.size() != 0){
            map.put("code",103);
            map.put("msg","该手机号已被注册");
            return map;
        }

        if(!tProxyInfo.getPwd1().equals(tProxyInfo.getPwd2())){
            map.put("code",104);
            map.put("msg","两次输入密码不一致");
            return map;
        }

        //手动写添加代理,返回代理主键ID
        TAdminInfo adminInfo = BackAdminLogin.getAdminInfo();
        if(adminInfo.getTaiGroupId() != null && adminInfo.getTaiGroupId() == 2){
            tProxyInfo.setTpiHeadId(adminInfo.getTaiRelationId());
        }else{
            tProxyInfo.setTpiHeadId(0);
        }
        tProxyInfo.setTpiStatus(1);
        String qrCode = wxUurl + "/#" + "/register" + "?qrCode=" + "P"+tProxyInfo.getTpiPhone().substring(tProxyInfo.getTpiPhone().length()-6,tProxyInfo.getTpiPhone().length());
        tProxyInfo.setTpiQrCode(qrCode);
        tProxyInfoMapper.insertTProxyInfo(tProxyInfo);


        //添加权限
        TAdminRole tAdminRole = new TAdminRole();
        tAdminRole.setTarGroupId(3);
        tAdminRole.setTarDesc(tProxyInfo.getTpiName());
        tAdminRoleMapper.insertRole(tAdminRole);

        //添加管理员信息
        TAdminInfo tAdminInfo = new TAdminInfo();
        tAdminInfo.setTaiGroupId(3);
        tAdminInfo.setTaiStatus(1);
        tAdminInfo.setTaiPwd(MD5Util.MD5(tProxyInfo.getPwd1()).toUpperCase());
        tAdminInfo.setTaiAccount(tProxyInfo.getTpiAccount());
        tAdminInfo.setTaiName(tProxyInfo.getTpiName());
        tAdminInfo.setTaiRelationId(tProxyInfo.getTpiId());
        tAdminInfo.setTaiRoleId(tAdminRole.getTarId());
        tAdminInfoMapper.insertSelective(tAdminInfo);
        return map;
    }

    @Transactional
    @Override
    public Map<String, Object> editTProxyInfo(TProxyInfo tProxyInfo) throws Exception {
        Map<String, Object> map = new HashMap<>();
        TAdminInfo adminInfo1 = BackAdminLogin.getAdminInfo();
        if(adminInfo1.getTaiGroupId() == null || adminInfo1.getTaiGroupId() == 3){
            map.put("code",105);
            map.put("msg","账号登录异常，请重新登录后操作！");
            return map;
        }
        if(tProxyInfo.getTpiId() == null){
            map.put("code",106);
            map.put("msg","代理ID不能为空");
        }
        //找到对应的管理员
        TAdminInfo tAdminInfo = tAdminInfoMapper.selectTProxyInfoByRelationId(tProxyInfo.getTpiId());

        if(!StringUtil.isBlank(tProxyInfo.getTpiAccount())){
            //验证账号是否存在
            List<TProxyInfo> accountList = tProxyInfoMapper.selectByAccount(tProxyInfo.getTpiAccount(), tProxyInfo.getTpiId());
            if(accountList.size() != 0){
                map.put("msg","该账号已被注册");
                map.put("code",102);
                return map;
            }
            if (tAdminInfo != null){
                tAdminInfo.setTaiAccount(tProxyInfo.getTpiAccount());
            }
        }

        if(!StringUtil.isBlank(tProxyInfo.getTpiPhone())){
            //验证手机号是否存在
            List<TProxyInfo> phoneList = tProxyInfoMapper.selectByPhone(tProxyInfo.getTpiPhone(), tProxyInfo.getTpiId());
            if(phoneList.size() != 0){
                map.put("msg","该手机号已被注册");
                map.put("code",103);
                return map;
            }
        }
        if (!StringUtil.isBlank(tProxyInfo.getTpiName()) && tAdminInfo != null){
            tAdminInfo.setTaiName(tProxyInfo.getTpiName());
        }
        if(tAdminInfo != null){
            tAdminInfoMapper.updateByPrimaryKeySelective(tAdminInfo);
        }

        tProxyInfoMapper.updateByPrimaryKeySelective(tProxyInfo);
        return map;
    }

    @Override
    public Map<String, Object> resetPwd(TProxyInfo tProxyInfo) {
        Map<String, Object> map = new HashMap<>();
        TAdminInfo adminInfo1 = BackAdminLogin.getAdminInfo();
        if(adminInfo1.getTaiGroupId() == null|| adminInfo1.getTaiGroupId() == 3){
            map.put("code",105);
            map.put("msg","账号登录异常，请重新登录后操作！");
            return map;
        }
        if(tProxyInfo.getTpiId() == null || StringUtil.isBlank(tProxyInfo.getPwd1()) || StringUtil.isBlank(tProxyInfo.getPwd2())){
            return ResultMap.errorParam();
        }

        //验证两次密码是否一样
        if(!tProxyInfo.getPwd2().equals(tProxyInfo.getPwd1())){
            map.put("msg","两次输入密码不一致");
            map.put("code",104);
            return map;
        }

        TAdminInfo tAdminInfo = tAdminInfoMapper.selectTProxyInfoByRelationId(tProxyInfo.getTpiId());
        tAdminInfo.setTaiPwd(MD5Util.MD5(tProxyInfo.getPwd1()).toUpperCase());
        tAdminInfoMapper.updateByPrimaryKeySelective(tAdminInfo);

        return map;
    }

    @Transactional
    @Override
    public Map<String, Object> updateStatus(TProxyInfo tProxyInfo) throws Exception {
        Map<String, Object> map = new HashMap<>();
        TAdminInfo adminInfo1 = BackAdminLogin.getAdminInfo();
        if(adminInfo1.getTaiGroupId() == null || adminInfo1.getTaiGroupId() == 3){
            map.put("code",105);
            map.put("msg","账号登录异常，请重新登录后操作！");
            return map;
        }
        if(tProxyInfo.getTpiId() == null || tProxyInfo.getTpiStatus() == null){
            return ResultMap.errorParam();
        }
        TProxyInfo newTProxyInfo = new TProxyInfo();
        newTProxyInfo.setTpiId(tProxyInfo.getTpiId());
        newTProxyInfo.setTpiStatus(tProxyInfo.getTpiStatus());
        tProxyInfoMapper.updateByPrimaryKeySelective(newTProxyInfo);

        TAdminInfo tAdminInfo = tAdminInfoMapper.selectTProxyInfoByRelationId(tProxyInfo.getTpiId());
        if(tAdminInfo != null){
            tAdminInfo.setTaiStatus(tProxyInfo.getTpiStatus());
            tAdminInfoMapper.updateByPrimaryKeySelective(tAdminInfo);
        }

        return map;
    }

    @Override
    public Map<String, Object> getProxyList(Integer centerId) {
        Map<String, Object> map = new HashMap<>();

        TAdminInfo adminInfo = BackAdminLogin.getAdminInfo();
        if(adminInfo.getTaiGroupId() == null || adminInfo.getTaiGroupId() == 3){
            map.put("data",null);
        }else{
            List<TProxyInfo> tpiList = tProxyInfoMapper.getProxyList(adminInfo.getTaiGroupId(), adminInfo.getTaiRelationId(),centerId);
            map.put("data",tpiList);
        }


        return map;
    }
}
